import { describe, expect, it } from 'vitest';
import { toJson } from '../../src/hammer/jsonFormat.mts';

describe('jsonFormat', () => {
  describe('toJson', () => {
    it('should return an object with only the specified keys', () => {
      const source = {
        name: 'John',
        age: 30,
        city: 'New York',
        country: 'USA',
      };

      const result = toJson(source, ['name', 'age']);

      expect(result).toEqual({
        name: 'John',
        age: 30,
      });

      // Check that it doesn't have other properties
      expect(Object.keys(result)).toHaveLength(2);
      expect(result).not.toHaveProperty('city');
      expect(result).not.toHaveProperty('country');
    });

    it('should return an empty object when no keys are specified', () => {
      const source = {
        name: 'John',
        age: 30,
      };

      const result = toJson(source, []);

      expect(result).toEqual({});
      expect(Object.keys(result)).toHaveLength(0);
    });

    it('should handle non-existent keys gracefully', () => {
      const source = {
        name: 'John',
        age: 30,
      };

      const result = toJson(source, ['name', 'nonExistent']);

      expect(result).toEqual({
        name: 'John',
        nonExistent: undefined,
      });
    });

    it('should preserve the type of values', () => {
      const source = {
        name: 'John',
        age: 30,
        isStudent: true,
        grades: [95, 87, 92],
        address: {
          street: '123 Main St',
          city: 'New York',
        },
      };

      const result = toJson(source, [
        'name',
        'age',
        'isStudent',
        'grades',
        'address',
      ]);

      expect(result).toEqual({
        name: 'John',
        age: 30,
        isStudent: true,
        grades: [95, 87, 92],
        address: {
          street: '123 Main St',
          city: 'New York',
        },
      });
    });
  });
});
